import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-26
 * Time:10:47
 */
public class Solution {
    // 插入排序
    public static void insertSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length; i++) {
            int cur=array[i];
            int j=0;
            while(array[j]<cur&&j<i){
                j++;
            }
            for (int k = i; k >j; k--) {
                array[k]=array[k-1];
            }
            array[j]=cur;
        }
    }

    // 希尔排序
    public static void shellSort(int[] array){
        // write code  here
        int gap=array.length/2;
        while(gap>0){
            int i=0;
            while(i+gap<array.length){
                if(array[i]>array[i+gap]){
                    int tmp=array[i];
                    array[i]=array[i+gap];
                    array[i+gap]=tmp;
                }
                i++;
            }
            gap/=2;
        }
    }

    // 选择排序
    public static void selectSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array.length; j++) {
                int min=j;
                if(array[j]<array[min]){
                    min=j;
                }
                int tmp=array[i];
                array[i]=array[min];
                array[min]=tmp;
            }

        }
    }
// 堆排序
    public static void heapSort(int[] array){
        // write code  here
        PriorityQueue<Integer> queue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        for (int i = 0; i < array.length; i++) {
            queue.add(array[i]);
        }
        for (int i = 0; i < array.length; i++) {
            array[i]=queue.poll();
        }
    }
    // 冒泡排序
    public static void bubbleSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
        }
    }

    // 快速
    public static void quickSort(int[] array){
        // write code  here
        Quick(array,0,array.length-1);
    }
    private static void Quick(int[] array, int left, int right){
        if(right-left<=1){
            return;
        }
        int mid=partition(array,left,right);
        Quick(array,left,mid-1);
        Quick(array,mid+1,right);
    }
    private static int partition(int[] array, int left, int right){

        int pivot=array[left];
        int l=left;
        int r=right;
        while(l<r){
            while(l<r&&array[r]>pivot){
                r--;
            }
            array[l]=array[r];
            while(l<r&&array[l]<pivot){
                l++;
            }
            array[r]=array[l];
        }
        array[l]=pivot;
        return l;
    }
    // 归并排序---递归
    public static void mergeSort(int[] array){
        // write code  here
        mergeSortChild(array,0,array.length-1);
    }
    public static void mergeSortChild(int []array,int left,int right){
        if(left>=right){
            return;
        }
        int mid=(right+left)/2;
        mergeSortChild(array,left,mid);
        mergeSortChild(array,mid+1,right);
        merge(array,left,right,mid);
    }
    public static void merge(int[]array,int left,int right,int mid){
        int l=left;
        int r=mid+1;
        while(l<=mid&&r<=right){
            if(array[l]>array[r]){
                int tmp=array[r];
                for (int i = r; i >l; i--) {
                    array[i]=array[i-1];
                }
                array[l]=tmp;
                r++;
            }
            l++;
        }

    }
// 归并排序---非递归
    public static void mergeSort1(int[] array){
        // write code  here
        Stack<Integer> stack=new Stack<>();
        int x=array.length;
        while(x>0){
            stack.push(x);
            x/=2;
        }
        while(!stack.isEmpty()){
            int i=stack.pop();
            int j=0;
            for (int k = 0; k < array.length; k+=i*2) {
                int left=k;
                int mid=left+i-1;
                int right=mid+i;
                if(mid>=array.length){
                    mid=array.length-1;
                }
                if(right>=array.length){
                    right=array.length-1;
                }
                merge(array,left,right,mid);
            }
        }
    }

// 计数排序
    public static void countSort(int[] array){
        // write code  here
        int min=array[0];
        int max=array[0];
        for (int i = 0; i < array.length; i++) {
            if(array[i]>max){
                max=array[i];
            }
            if(array[i]<min){
                min=array[i];
            }
        }
        int[] arr=new int[max-min+1];
        for (int i = 0; i < array.length; i++) {
            arr[array[i]-min]++;
        }
        int j=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==1){
                array[j++]=i+min;
            }
        }
    }
    public static void main(String[] args) {
        int array[]={9,8,7,6,5,4,3,2};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
}
